// pages/shopCart/shopCart.js
Page({
  data: {
    allPrice: 0,
    headerStyle:"",
    seletcId: 0,
    oderList:[],
    MenuList:[
      {
        id: 0,
        tag:"满减",
        tagTips:"每满49.9减9.5，最高优惠100元，满2件加赠，满4件加赠，赠完为止",
        img: "/images/banner-3.jpg",
        title: "【甜蜜日晒】瑞幸春日挂耳咖啡 0蔗糖添加醇享黑咖啡（7杯/盒）",
        numDesc: "1盒（10g * 7袋",
        price: 8,
        num: 1,
        status: 0,
        show: 0
      },
      {
        id: 1,
        tag:"满减",
        tagTips:"每满49.9减9.5，最高优惠100元，满2件加赠，满4件加赠，赠完为止",
        img: "/images/banner-3.jpg",
        title: "【甜蜜日晒】瑞幸春日挂耳咖啡 0蔗糖添加醇享黑咖啡（7杯/盒）",
        numDesc: "1盒（10g * 7袋",
        price: 29.9,
        num: 1,
        status: 0,
        show: 0
      },
      {
        id: 2,
        tag:"满减",
        tagTips:"每满49.9减9.5，最高优惠100元，满2件加赠，满4件加赠，赠完为止",
        img: "/images/banner-3.jpg",
        title: "【甜蜜日晒】瑞幸春日挂耳咖啡 0蔗糖添加醇享黑咖啡（7杯/盒）",
        numDesc: "1盒（10g * 7袋",
        price: 29.9,
        num: 1,
        status: 0,
        show: 0
      },
      {
        id: 3,
        tag:"满减",
        tagTips:"每满49.9减9.5，最高优惠100元，满2件加赠，满4件加赠，赠完为止",
        img: "/images/banner-3.jpg",
        title: "【甜蜜日晒】瑞幸春日挂耳咖啡 0蔗糖添加醇享黑咖啡（7杯/盒）",
        numDesc: "1盒（10g * 7袋",
        price: 29.9,
        num: 1,
        status: 0,
        show: 0
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCenterPosition()
    this.sumAllPrice()
      wx.getStorage({
      key:"orderRecord",
      success: res => {
          console.log(res.data)
          this.setData({
            orderList:res.data
          })
      }
  })
  },
  getCenterPosition(){
    // 获取胶囊的样式内容
    let {top,height} =  wx.getMenuButtonBoundingClientRect();
    // console.log(wx.getMenuButtonBoundingClientRect())
    let centerPosition = top + height / 2;
    let headerStyle = "margin-top:calc(" + centerPosition + "px - 29rpx";
    this.setData({
      headerStyle:headerStyle
    })
  },
  // 商品选中
  chooseShop(event){
    // console.log(event)
    let {sid} = event.currentTarget.dataset
    // console.log(sid)
    let newstatus = this.data.MenuList[sid].status
    // console.log(newstatus)
    let newList = this.data.MenuList
    if(newstatus == 0){
      newstatus = 1
      newList[sid].status = newstatus
      this.setData({
        MenuList:newList
      })
    }
    else{
      newstatus = 0
      newList[sid].status = newstatus
      this.setData({
        MenuList:newList
      })
    }
  },
  // 增加商品数量
  addNum(event){
    let {addid} = event.currentTarget.dataset
    let newNum = this.data.MenuList[addid].num
    newNum ++
    let newList = this.data.MenuList
    newList[addid].num = newNum
    this.setData({
      MenuList: newList
    })
    this.sumAllPrice()
    // console.log(newList[addid].num)
  },
  // 减少商品数量
  subNum(event){
    // console.log(event)
    let {subid} = event.currentTarget.dataset
    // console.log(addid)
    let newNum = this.data.MenuList[subid].num
    let newList = this.data.MenuList
    let showStatus = this.data.MenuList[subid].show
    if(newNum > 1){
      newNum --
      newList[subid].num = newNum
      this.setData({
        MenuList: newList
      })
      this.sumAllPrice()
    }else{
      showStatus = 1
      newNum = 0
      newList[subid].num = newNum
      newList[subid].show = showStatus
      this.setData({
        MenuList: newList
      })
      this.sumAllPrice()
    }
    
  },
  // 计算价格
  sumAllPrice(){
    let newList = this.data.MenuList
    let num = 0
    let allPrice = 0
    for (const model of newList) {
      if(model.num > 0){
        num += model.num
        allPrice += model.price * model.num
      }
    }
    this.setData({
      allPrice: allPrice,
    })

  },
  goList(){
    wx.navigateTo({
      url: '/pages/myOrder/myOrder',
    })
  },

  ret(){
    wx.switchTab({
      url: '/pages/shop/shop',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.getStorage({
      key:"orderRecord",
      success: res => {
          console.log(res.data)
          this.setData({
            orderList:res.data
          })
      }
  }) 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})